package aryn.framework.display;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class MouseMotionListenerAdapter extends MouseMotionAdapter {

	private int width;
	private int height;
	private MyMouseInfo info;
	
	public MouseMotionListenerAdapter(MyCanvas canvas) {
		this.width = canvas.getWidth();
		this.height = canvas.getHeight();
		this.info = canvas.getMouseInfo();
	}

	public void mouseDragged(MouseEvent e) {
		setMouse(e);
		info.setDragged(true);
	}
	
	public void mouseMoved(MouseEvent e) {
		setMouse(e);
		info.setMoved(true);
	}

	private void setMouse(MouseEvent e) {
		info.reset();
		int mx = e.getX();
		int my = e.getY();
		int gmx = e.getXOnScreen();
		int gmy = e.getYOnScreen();
		if(mx < 0) mx = 0;		if(mx > width) mx = width;
		if(my < 0) my = 0;		if(my > height) my = height;
		if(gmx < 0) gmx = 0;	if(gmx > width) gmx = width;
		if(gmy < 0) gmy = 0;	if(gmy > height) gmy = height;
		info.setMouseX(mx);
		info.setMouseY(my);
		info.setGlobalMouseX(gmx);
		info.setGlobalMouseY(gmy);
	}
}
